import { getStore, setStore } from '@/util/store'
import { diff } from '@/util/util'

const tagObj = {
  label: '', // 标题名称
  value: '', // 标题的路径
  params: '', // 标题的路径参数
  query: '', // 标题的参数
  group: [], // 分组
  parentId: '' //父级菜单Id
}
const tagWel = {
  label: '首页',
  value: '/home',
  params: {},
  query: {},
  group: [],
  close: false
}


// 处理首个标签
function setFistTag(list) {
  if (list.length === 1) {
    list[0].close = false
  } else {
    list.forEach(ele => {
      if (ele.value === tagWel.value ) {
        ele.close = false
      } else {
        ele.close = true
      }
    })
  }
}


const navs = {
  state: {
    tagList: getStore({ name: 'tagList' }) || [],    //tag列表页
    tag: getStore({ name: 'tag' }) || tagObj,        //当前tag页
    tagWel: tagWel,                                 
  },
  actions: {},
  mutations: {
    ADD_TAG: (state, action) => {
      state.tag = action
      setStore({ name: 'tag', content: state.tag })
      if (state.tagList.some(ele => diff(ele, action))) return
      // 过滤路由相同，但是传参不同
      state.tagList.forEach((item,i)=>{
        if(item.label == action.label){
          state.tagList.splice(i,1)
        }
      })
      state.tagList.push(action)
      setFistTag(state.tagList)
      setStore({ name: 'tagList', content: state.tagList})
    },
    DEL_TAG: (state, action) => {
      state.tagList = state.tagList.filter(item => {
        return !diff(item, action)
      })
      setFistTag(state.tagList)
      setStore({ name: 'tagList', content: state.tagList })
    },
    DEL_ALL_TAG: (state) => {
      state.tagList = [state.tagWel]
      setStore({ name: 'tagList', content: state.tagList })
    },
    DEL_TAG_OTHER: (state) => {
      state.tagList = state.tagList.filter(item => {
        console.log(state.tag.value);
        if (item.value === state.tag.value) {
          return true
        }
      })
      console.log(state.tagList,1);
      setFistTag(state.tagList)
      setStore({ name: 'tagList', content: state.tagList })
    }
  }
}
export default navs









